{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cf17a69",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "import random\n",
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 设置数据集路径\n",
    "dataset_path = r\"D:\\desktop\\class\\DataSet\\dogs-vs-cats\\train\"\n",
    "print(\"数据集路径:\", dataset_path)\n",
    "\n",
    "# 查看文件夹内容\n",
    "if os.path.exists(dataset_path):\n",
    "    files = os.listdir(dataset_path)\n",
    "    print(f\"找到 {len(files)} 个文件\")\n",
    "    \n",
    "    # 显示前10个文件名\n",
    "    print(\"前10个文件:\")\n",
    "    for i, file in enumerate(files[:10]):\n",
    "        print(f\"  {i+1}. {file}\")\n",
    "else:\n",
    "    print(\"数据集路径不存在，请检查路径\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "796fea9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def analyze_file_patterns(files):\n",
    "    \"\"\"分析文件名模式来确定标签\"\"\"\n",
    "    cat_files = []\n",
    "    dog_files = []\n",
    "    other_files = []\n",
    "    \n",
    "    for file in files:\n",
    "        if file.lower().startswith('cat'):\n",
    "            cat_files.append(file)\n",
    "        elif file.lower().startswith('dog'):\n",
    "            dog_files.append(file)\n",
    "        else:\n",
    "            other_files.append(file)\n",
    "    \n",
    "    print(f\"猫图片数量: {len(cat_files)}\")\n",
    "    print(f\"狗图片数量: {len(dog_files)}\")\n",
    "    print(f\"其他文件数量: {len(other_files)}\")\n",
    "    \n",
    "    if other_files:\n",
    "        print(\"其他文件示例:\", other_files[:5])\n",
    "    \n",
    "    return cat_files, dog_files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9420b40",
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_labeled_dataset(source_dir, target_base_dir):\n",
    "    \"\"\"\n",
    "    为混在一起的数据集创建标签和组织结构\n",
    "    \n",
    "    Args:\n",
    "        source_dir: 原始数据目录\n",
    "        target_base_dir: 目标基础目录\n",
    "    \"\"\"\n",
    "    \n",
    "    # 创建目标目录结构\n",
    "    directories = [\n",
    "        os.path.join(target_base_dir, 'cats'),\n",
    "        os.path.join(target_base_dir, 'dogs'),\n",
    "    ]\n",
    "    \n",
    "    for directory in directories:\n",
    "        os.makedirs(directory, exist_ok=True)\n",
    "        print(f\"创建目录: {directory}\")\n",
    "    \n",
    "    # 获取所有图片文件\n",
    "    all_files = [f for f in os.listdir(source_dir) \n",
    "                if f.lower().endswith(('.jpg', '.jpeg', '.png'))]\n",
    "    \n",
    "    # 分离猫和狗图片\n",
    "    cat_images = [f for f in all_files if f.lower().startswith('cat')]\n",
    "    dog_images = [f for f in all_files if f.lower().startswith('dog')]\n",
    "    \n",
    "    print(f\"\\n找到 {len(cat_images)} 张猫图片\")\n",
    "    print(f\"找到 {len(dog_images)} 张狗图片\")\n",
    "    \n",
    "    # 随机打乱并分割\n",
    "    random.shuffle(cat_images)\n",
    "    random.shuffle(dog_images)\n",
    "    \n",
    "    \n",
    "    # 复制文件到相应目录\n",
    "    print(\"\\n开始复制文件...\")\n",
    "    \n",
    "    # 处理猫图片\n",
    "    for i, filename in enumerate(cat_images):\n",
    "        src_path = os.path.join(source_dir, filename)\n",
    "        dst_path = os.path.join(target_base_dir,'cats', filename)\n",
    "        shutil.copy2(src_path, dst_path)\n",
    "    \n",
    "    # 处理狗图片\n",
    "    for i, filename in enumerate(dog_images):\n",
    "        src_path = os.path.join(source_dir, filename)\n",
    "        dst_path = os.path.join(target_base_dir,'dogs', filename)\n",
    "        shutil.copy2(src_path, dst_path)\n",
    "    \n",
    "    print(\"文件复制完成!\")\n",
    "    \n",
    "    # 统计结果\n",
    "    train_cats = len(os.listdir(os.path.join(target_base_dir,'cats')))\n",
    "    train_dogs = len(os.listdir(os.path.join(target_base_dir,'dogs')))\n",
    "    \n",
    "    print(f\"\\n最终数据分布:\")\n",
    "    print(f\"训练集 - 猫: {train_cats} 张, 狗: {train_dogs} 张\")\n",
    "    \n",
    "    return {\n",
    "        'train_cats': train_cats,\n",
    "        'train_dogs': train_dogs,\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34432ccd",
   "metadata": {},
   "outputs": [],
   "source": [
    "cat_files, dog_files = analyze_file_patterns(files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "59cde7e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "最终数据分布:\n",
      "训练集 - 猫: 12500 张, 狗: 12500 张\n"
     ]
    }
   ],
   "source": [
    "target_directory = r\"D:\\desktop\\class\\DataSet\\cats_vs_dogs_labeled\"\n",
    "# stats = create_labeled_dataset(dataset_path, target_directory) //调用一次就行，已经生成了对应的图片了\n",
    "train_cats = len(os.listdir(os.path.join(target_directory,'cats')))\n",
    "train_dogs = len(os.listdir(os.path.join(target_directory,'dogs')))\n",
    "print(f\"\\n最终数据分布:\")\n",
    "print(f\"训练集 - 猫: {train_cats} 张, 狗: {train_dogs} 张\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ba7f465",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
